package com.wt.struggle.goodbye.user.adapter

import android.content.Context
import android.os.Bundle
import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import com.wt.struggle.goodbye.R
import com.wt.struggle.goodbye.base.Config
import com.wt.struggle.goodbye.base.ProBaseAdapter
import com.wt.struggle.goodbye.info.NewsInfo
import com.xin.lv.yang.utils.utils.ImageUtil
import kotlinx.android.synthetic.main.manager_item.view.*

class ManagerAdapter(context: Context, list: ArrayList<NewsInfo>) : ProBaseAdapter<NewsInfo>(context, list) {

    var code = 1

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val vh = holder as VH
        val info = list[position]
        show(vh, info)

    }


    fun show(vh: VH, info: NewsInfo) {

        ImageUtil.getInstance().loadRoundCircleImage(context, vh.cycleImageHead, Config.GET_IMAGE_URL + info.icon, 0, 4)
        vh.tvCycleName.text = info.name
        vh.tvCycleMs.text = info.des
        vh.cbCollectNum.text = info.numberOfCollected.toString()
        vh.cbCommentNum.text = info.numberOfCommented.toString()

        if (code == 1) {
            vh.cycleCheckBox.visibility = View.VISIBLE
        } else {
            vh.cycleCheckBox.visibility = View.GONE
        }

        vh.cycleCheckBox.isChecked = false

        vh.cycleCheckBox.setOnCheckedChangeListener { _, b ->
            change(b, vh.adapterPosition)
        }
    }

    private fun change(b: Boolean, position: Int) {
        val info = list[position]
        info.isCheck = b

    }

    override fun onCreateView(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view = inflater.inflate(R.layout.manager_item, parent, false)
        return VH(view)
    }

    override fun onUpdateHolder(holder: RecyclerView.ViewHolder, position: Int, payloads: MutableList<Any>) {
        val bundle = payloads[0] as Bundle
        val set = bundle.keySet()
        for (temp in set) {
            when (temp) {
                "info" -> {
                    val info: NewsInfo = bundle.getParcelable(temp)
                    show(holder as VH, info)
                }
            }
        }

    }

    class VH(view: View) : RecyclerView.ViewHolder(view) {
        val cycleCheckBox = view.cycleCheckBox
        val cycleImageHead = view.cycleImageHead
        val tvCycleName = view.tvCycleName
        val tvCycleMs = view.tvCycleMs
        val cbCollectNum = view.cbCollectNum
        val cbCommentNum = view.cbCommentNum

    }
}